<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_ProductKeyController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        parent::loadTemplate('alpona');
        $this->view->headTitle = "manage product key";
	}
	
	public function indexAction(){
	    $scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
		$model = new Admin_Model_ProductKey();
        $keys = $model->getDatas(array('where' => 'status = 1'), array('id', 'product', 'key', 'created', 'created_by'), true);
        
        $this->view->data = $keys;
	}
    
    public function createAction(){
    	$request = $this->_request;
        
        $product_model = new Admin_Model_Product();
        $products = $product_model->getDatas(array('where' => 'status = 1'), array('p.id', 'name'));
        
        if($request->isPost()){
            $validator = new Admin_Form_ValidateProductKey($request->getParams());
            
            if($validator->isError()){
                $this->view->isError = $validator->isError();
                $this->view->messageError = $validator->getMessageError();
                $this->view->classError = $validator->getClassError();
                $this->view->data = $validator->getData();
            }else{
                $data = $validator->getData();
                
                $model = new Admin_Model_ProductKey();
                $rs = $model->saveData($data);
                
                if($rs){
                    $product = $product_model->getData($id, array('id', 'quantity'));
                    $product['quantity'] += 1;
                    
                    $rs = $product_model->saveData($product);
                }
                
                //$this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'product-key', 'action' => 'index'), null, true);
            }
        }
        
        $this->view->products = $products;
    }
    
    public function editAction(){
        $request = $this->_request;
        
        if($request->isPost()){
            $model = new Admin_Model_ProductKey();
            $key = $model->getData($request->getParam('id'));
            
            $product_model = new Admin_Model_Product();
            $products = $product_model->getDatas(array('where' => 'status = 1'), array('p.id', 'name'));
            
            $this->view->data = $key;
            $this->view->products = $products;
            
            $save = $request->getParam('save', false);
            
            if($save){
                $validator = new Admin_Form_ValidateProductKey($request->getParams(), true, $key);
            
                if($validator->isError()){
                    $this->view->isError = $validator->isError();
                    $this->view->messageError = $validator->getMessageError();
                    $this->view->classError = $validator->getClassError();
                    $this->view->data = $validator->getData();
                }else{
                    $rs = $model->saveData($validator->getData());
                    
                    $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'product-key', 'action' => 'index'), null, true);
                }
            }
        }
    }
    
    public function updateStatusAction(){
    	$request = $this->_request;
        
        if($request->isPost()){
            $params = $request->getParams();
            
            $data['id'] = $params['id'];
            
            if($params['status'] == 'active'){
                $data['status'] = true;
            }else{
                $data['status'] = false;
            }
            
            $model = new Admin_Model_ProductKey();
            $rs = $model->saveData($data);
        }
        
        $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'product-key', 'action' => 'index'), null, true);
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function deleteAction(){
        $request = $this->_request;
        
        if($request->isPost()){
            $id = $request->getParam('id');
            
            $model = new Admin_Model_ProductKey();
            $key = $model->getData($id);
            
            $product_id = $key['product'];
            
            $rs = $model->deleteData($id);
            
            if($rs){
                $product_model = new Admin_Model_Product();
                $product = $product_model->getData($product_id, array('id', 'quantity'));
                $product['quantity'] -= 1;
                
                $rs = $product_model->saveData($product);
            }
        }
        
        $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'product-key', 'action' => 'index'), null, true);
        
        $this->_helper->viewRenderer->setNoRender();
    }
}